﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace SimpleUnitity.Cache
{
    [Serializable]
    public class SessionCache : WarpCache, ICache
    {
        private SessionCache(ICache cache)
            : base(cache)
        {
        }

        static readonly string sessionKey = string.Format("__SimpleUnitity_Cache_SessionCache_Current_RANDON_{0}_", new Random().NextDouble());

        public static ICache Current
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    return null;
                }
                NormalCache cache = HttpContext.Current.Session[sessionKey] as NormalCache;
                if (cache == null)
                {
                    HttpContext.Current.Session[sessionKey] = cache = new NormalCache();
                }
                return new SessionCache(cache);
            }
        }
    }
}
